package com;

import java.sql.*;

/*
执行DQL语句
 */
public class Test4 {
    public static void main(String[] args) {
        Connection conn=null;//定义语句链接对象
        Statement ste=null;//定义sql语句执行对象
        ResultSet resultSet=null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            //注册包
            try {//
                conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/db1", "root", "root");
                // 获取数据链接对象
                String sql="select*from worker1";
                //定义sql语句
                ste = conn.createStatement();//获取sql执行对象
             resultSet = ste.executeQuery(sql);
                  resultSet.next();
                int i = resultSet.getInt(1);
                String string = resultSet.getString(2);
                String adress = resultSet.getString("adress");
                System.out.println(i);
                System.out.println(string);
                System.out.println(adress);

            } catch (SQLException e) {
                e.printStackTrace();
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();//
        }finally{//释放资源
            if(resultSet!=null){
                try {
                    resultSet.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if(ste!=null){
                try {
                    ste.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if(conn!=null){
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }

        }
    }
    }


